* This do-file creates a figure depicting adoption and area planted by year and season
* Paper Figure 4

tempfile treats

*****
* Household treatment assignemnts
*****

use "$out_data/survey_roster.dta", clear
merge m:1 village using "$admin_data/treatment_assignment.dta", keepusing(treat_input treat_idi) assert(match using)
drop if _m==2
drop _m
merge m:1 hhid using "$out_data/survey_drops.dta", assert(match) nogen

// Make year*treat interactions
expand 3
bys hhid: gen byte year = _n
* Dummy out treatment interactions
gen byte B_y2 = year==2
label var B_y2 "Year 2"
gen byte B_y3 = year==3
label var B_y3 "Year 3"
gen byte B_y1_t = (year==1)*(treat_input==1)
label var B_y1_t "Treat Yr. 1"
gen byte B_y2_t = (year==2)*(treat_input==1)
label var B_y2_t "Treat Yr. 2"
gen byte B_y3_t = (year==3)*(treat_input==1)
label var B_y3_t "Treat Yr. 3"

save `treats'

*****
* Adoption and Area
*****
* Regression and Figure
* Kharif adoption (dummy), Kharif area, Rabi adoption (dummy), Rabi area, Zaid adoption, Zaid area
//use `treats', clear
//merge 1:m hhid year using "$out_data/outcome_adoption_production.dta", assert(match) nogen
use "$out_data/outcome_adoption_production.dta", clear
collapse (sum) pulse_area, by(hhid year season) fast
merge m:1 hhid year using `treats', assert(match) nogen

* Make adoption dummy
gen byte pulse_adopt = pulse_area>0 if !missing(pulse_area)


* Main regression
forvalues s = 1/3 {
foreach vbl in adopt area {
	if (`s'==3) local samp ever
	else local samp srvy
	
	reg pulse_`vbl' $B_main $controls if season==`s' & drop_`samp'==0, vce(cluster village)
	mat b_`s'_`vbl' = e(b)
	mat e_`s'_`vbl' = vecdiag(e(V))
	//mat e_`s'_`vbl' = diag(temp)
	
	sum pulse_`vbl' if season==`s' & year==1 & treat_input==0
	local u_`s'_`vbl' = `r(mean)'
}
}

local grset graphregion(color(white)) plotregion(margin(sides))
graph drop _all

clear
set obs 3
gen byte year = _n
label define yrs 1 "Year 1" 2 "Year 2" 3 "Year 3"
label values year yrs
local comb ""

forvalues s = 1/3 {
foreach vbl in adopt area {
	local spec "`s'_`vbl'"
	
	mat temp = (0\b_`spec'[1..1,4..5]')
	svmat temp
	replace temp1 = temp1 + `u_`spec''
	rename temp1 b0_`spec'
	
	mat temp = b_`spec'[1..1,1..3]'
	svmat temp
	replace temp1 = temp1 + b0_`spec'
	rename temp1 bt_`spec'
	
	
	mat temp = e_`spec'[1..1,1..3]'
	svmat temp
	replace temp = sqrt(temp)
	rename temp1 se_`spec'
	
	gen hi_`spec' = bt_`spec' + 1.96*se_`spec'
	gen lo_`spec' = max(bt_`spec' - 1.96*se_`spec',0)
	
	
	local xset xlabel(1 "Year 1" 2 "Year 2" 3 "Year 3", angle(0) noticks) xtitle("")
	if "`vbl'"=="adopt" {
			local opts  ylabel(0(0.2)0.8, angle(0) gmin gmax) 
			if `s'==1 {
				local tit "Kharif"
				local ytit ytitle("Share planting pulses")
			}
			else if `s'==2 {
				local tit "Rabi"
				local ytit ytitle("")
			}
			else if `s'==3 {
				local tit "Zaid"
				local ytit ytitle("")
			}
	}
	else if "`vbl'"=="area" {
			local opts ylabel(0(0.1)0.4, angle(0) gmin gmax) 	
			local tit ""
			if `s'==1 local ytit ytitle("Avg. pulse area planted (acres)")
			else if `s'>1 local ytit ytitle("")
	}
	
	twoway (rcap lo_`spec' hi_`spec' year, color(gs0) lwidth(thin)) ///
			(connected b0_`spec' year, color(gs0) msymbol(o) lwidth(thin)) ///
			(connected bt_`spec' year, color(gs0) msymbol(t) lpattern(dash)) ///
			, `grset' `opts' `xset' `ytit' title("`tit'") name(g_`spec') ///
			legend(order(2 "Control" 3 "Treated")) 
			
			graph export "$out_figures/Parts/fig_4`spec'.png", replace
			
}
}

grc1leg g_1_adopt g_2_adopt g_3_adopt g_1_area g_2_area g_3_area, graphregion(color(white)) cols(3)
graph export "$out_figures/Figure_4.png", replace

